package com.flymobi.mshop.adapter;

import java.io.IOException;
import java.io.InputStream;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.flymobi.mshop.R;
import com.flymobi.mshop.util.ImageUtil;
import com.flymobi.mshop.util.service.IService;

/**
 * 
 * @author 余鹏
 * Gallery的3D效果适配器
 */

public class Image3dAdapter extends BaseAdapter {

	int mGalleryItemBackground;

	private Context mContext;

	private String[] mImagePaths;

	private Drawable m_Drawable = null;
	private Bitmap m_Bitmap = null;

	public Image3dAdapter(Context c, String[] strImagePath) {

		mContext = c;

		mImagePaths = strImagePath;

	}

	public int getCount() {

		return mImagePaths.length;
	}

	public Object getItem(int position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}

	class ViewHolder {
		ImageView imageViewShow;
	}

	public View getView(int position, View convertView, ViewGroup parent) {

		try {

			final ViewHolder holder;
			if (convertView == null) {
				convertView = View.inflate(mContext, R.layout.gallery_item,
						null);
				holder = new ViewHolder();
				holder.imageViewShow = (ImageView) convertView
						.findViewById(R.id.imageViewShow);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			holder.imageViewShow.setImageDrawable(getDrawable(position));

			convertView.setTag(holder);
			return convertView;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return convertView;

	}

	/*public float getScale(boolean focused, int offset) {

		 Formula: 1 / (2 ^ offset) 

		return Math.max(0, 1.0f / (float) Math.pow(2, Math.abs(offset)));

	}*/

	private Drawable getDrawable(int intIndex) {

		// ������Ҫ�Ĳ��֮���ʡ˼��ԭʼͼ��

		final int reflectionGap = 10;
		m_Drawable = null;

		String strPath = mImagePaths[intIndex];

		if (strPath != null && strPath.length() > 0) {
			InputStream stream = null;
			BitmapFactory.Options opt = null;
			try {
				/*FileInputStream inputstream = new FileInputStream(strPath);

				opt = new BitmapFactory.Options();
				opt.inSampleSize = 15;
				inputstream = new FileInputStream(strPath);
				stream = new BufferedInputStream(inputstream);
				m_Bitmap = BitmapFactory.decodeStream(stream, null, opt);
				inputstream.close();*/
				m_Bitmap=ImageUtil.getHttpBitmap(IService.serverUrl + strPath);

				int width = m_Bitmap.getWidth();

				int height = m_Bitmap.getHeight();

				// ���Ⲣ���Ὣ��ģ����Y����

				Matrix matrix = new Matrix();

				matrix.preScale(1, -1);

				// ����һ��λͼ�뷭ת����,�������⡣

				// ����ֻ��Ҫ�ײ�һ�������

				Bitmap reflectionImage = Bitmap.createBitmap(m_Bitmap, 0,

				height / 2, width, height / 2, matrix, false);

				// ����һ���µ�λͼ�Լ���ͬ��ȵ���ߵĸ����

				// ����

				Bitmap bitmapWithReflection = Bitmap.createBitmap(width,

				(height + height / 2), Config.ARGB_8888);

				Canvas canvas = new Canvas(bitmapWithReflection);

				// ����ԭʼͼ��
				canvas.drawBitmap(m_Bitmap, 0, 0, null);

				// ���ڷ�

				Paint deafaultPaint = new Paint();

				canvas.drawRect(0, height, width, height + reflectionGap,

				deafaultPaint);

				canvas.drawBitmap(reflectionImage, 0, height + reflectionGap,
						null);

				Paint paint = new Paint();

				LinearGradient shader = new LinearGradient(0, m_Bitmap

				.getHeight(), 0, bitmapWithReflection.getHeight()

				+ reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);

				paint.setShader(shader);

				paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));

				// ��һ��������ʹ�����������ǵ������ݶ�

				canvas.drawRect(0, height, width, bitmapWithReflection
						.getHeight()

						+ reflectionGap, paint);

				// ���ͼƬ�ľ������

				BitmapDrawable bd = new BitmapDrawable(bitmapWithReflection);

				bd.setAntiAlias(true);
				m_Drawable = (Drawable) bd;

			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				if (stream != null) {
					try {
						stream.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}

				opt = null;
			}
		}

		return m_Drawable;
	}

}